﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using System.Collections;

namespace VehicleManagement.ControllersServices
{
    public class AddGasService
    {
        public IList<AddGas> GetAllAddGas()
        {
            IList<AddGas> lstModel = SqlMapperManager.Instance.Get().QueryForList<AddGas>("SELECT_ADDGAS", null);
            return lstModel;
        }

        // GET: /AddGas/Details/5
        /// <summary>
        /// 根据加油ID，查看加油详细信息，并返回视图
        /// </summary>
        /// <param name="id">加油的ID</param>
        /// <returns>加油信息详细视图</returns>
        public AddGas AddGasDetail(int id)
        {
            IList<AddGas> model = SqlMapperManager.Instance.Get().QueryForList<AddGas>("SELECT_AddGAS_BY_ID", id);
            AddGas addgas = model.Where(a => a.AddGasID == id).First();
            return addgas;
        }

        public void CreateAddGas(AddGas addGas)
        {
            object obj = SqlMapperManager.Instance.Get().Insert("INSERT_ADDGAS", addGas);
        }

        public AddGas ShowEdit(int id)
        {
            IList<AddGas> model = SqlMapperManager.Instance.Get().QueryForList<AddGas>("SELECT_AddGAS_BY_ID", id);
            AddGas addgas = model.Where(a => a.AddGasID == id).First();
            return addgas;
        }

        /// <summary>
        /// 车加油信息修改
        /// </summary>
        /// <param name="id">车加油ID</param>
        /// <param name="driveriofo">返回车加油类</param>
        /// <returns>修改车加油的视图</returns>
        public void EditAddGas(AddGas addgas)
        {
            CarInfoServices cis = new CarInfoServices();
            DriverServices ds = new DriverServices();
            addgas.CarID = cis.GetCarIdByCarNum(addgas.Car_num);
            addgas.DriverID = ds.GetDriverIdByDriverName(addgas.Driver_name);

            IList<AddGas> model = SqlMapperManager.Instance.Get().QueryForList<AddGas>("UPDATE_AddGAS", addgas);       
           //return  SqlMapperManager.Instance.Get().Update("UPDATE_AddGAS",addgas);
        }

        public int DeleteAddGas(int id)
        {
            return SqlMapperManager.Instance.Get().Delete("DELETE_AddGAS", id);
        }


        public object InsertAddGasEx(AddGas addgas)
        {
            convertAddGasForDatabase(ref addgas);
            return InsertAddGas(addgas);
        }
        public object InsertAddGas(AddGas addgas)
        {
            return SqlMapperManager.Instance.Get().Insert("INSERT_ADDGAS", addgas);
        }

        private void convertAddGasForDatabase(ref AddGas addgas)
        {
            CarInfoServices cis = new CarInfoServices();
            DriverServices ds = new DriverServices();
            addgas.CarID = cis.GetCarIdByCarNum(addgas.Car_num);
            addgas.DriverID = ds.GetDriverIdByDriverName(addgas.Driver_name);
        }

        public IList<AddGas> GetAllAddGas(Hashtable ht)
        {
            IList<AddGas> lstAddgas = new List<AddGas> { };
                lstAddgas = SqlMapperManager.Instance.Get().QueryForList<AddGas>("SEARCH_AddGAS", ht);
            return lstAddgas;

        }

        public int CountAllAddGas(Hashtable ht)
        {
            IList<AddGas> lstAddgas = new List<AddGas> { };
            lstAddgas = SqlMapperManager.Instance.Get().QueryForList<AddGas>("SEARCH_AddGAS", ht);
            return lstAddgas.Count;
        }

        public IList<AddGas> GetAllAddGas(Hashtable ht, int skip, int max)
        {
            IList<AddGas>  lstAddgas = SqlMapperManager.Instance.Get().QueryForList<AddGas>("SEARCH_AddGAS", ht,skip,max);
            return lstAddgas;
        }
    }
}